package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.luck.picture.lib.config.PictureMimeType;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.guards.R;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName :
 * @Author : wkk
 * @Date : 2021-5-31 17:05:29
 * @Email : 1281573890@qq.com
 * @Eescription :
 */

/**
 * 自定义布局，图片
 */
public class InfraredRecAdapter extends BannerAdapter<String, InfraredRecAdapter.ImageHolder> implements View.OnClickListener {


    private OnItemClickListener mOnItemClickListener = null;
    private Context context;

    public InfraredRecAdapter(Context context, List<String> mDatas) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
        this.context = context;
    }

    //更新数据
    public void updateData(List<String> data) {
        //这里的代码自己发挥，比如如下的写法等等
        mDatas.clear();
        mDatas.addAll(data);
        notifyDataSetChanged();
    }


    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public ImageHolder onCreateHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_infrared_rec_view, parent, false);
        // 实例化viewholder
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);
        ImageHolder viewHolder = new ImageHolder(view);
//        if (mDatas.size() == 1) {
//            viewHolder.viewBlock.setVisibility(View.GONE);
//            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) viewHolder.layout.getLayoutParams();
//            lp.setMargins(0, 0, 0, SizeUtils.dp2px(10));
//            viewHolder.layout.setLayoutParams(lp);
//        }
        return viewHolder;
    }

    @Override
    public void onBindView(ImageHolder holder, String data, int position, int size) {
        String url = data;
        boolean isHasVideo = PictureMimeType.isUrlHasVideo(url);
        if (isHasVideo) {
            holder.ivVideo.setVisibility(View.VISIBLE);
        } else {
            holder.ivVideo.setVisibility(View.GONE);
        }

        Glide.with(context)
                .load(url)
                .apply(BaseApplication.getRequestOptions())
                .into(holder.ivPicture);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ImageHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_picture)
        ShapeableImageView ivPicture;
        @BindView(R.id.iv_video)
        ImageView ivVideo;
        @BindView(R.id.layout)
        FrameLayout layout;

        public ImageHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
